<?php

    class ElementTableBrowseTitle extends Element {
        public function Render() {
            /* TODO: sortby */
            global $table;

            $fields = $table->Fields;
            $indexes = $table->Indexes;

            $fieldnames = array_keys( $fields );
            $sortByField = New DBField();

            $primaryFields = array();
            foreach ( $indexes as $index ) {
                if ( $index->Type == DB_KEY_PRIMARY ) {
                     foreach ( $index->Fields as $field ) {
                         $primaryFields[ $field->Name ] = $field;
                     }
                }
            }

            $sortByOrder = 'asc';

            ?><tr class="title"><?php
                foreach ( $fields as $field ) {
                    $isprimary = isset( $primaryFields[ $field->Name ] );

                    ?><th <?php
                    if ( $field->Name == $sortByField->Name || $isprimary ) {
                        ?>class="<?php
                        if ( $field->Name == $sortByField->Name ) {
                            ?>sort <?php
                        }
                        if ( $isprimary ) {
                            ?>primary<?php
                        }
                        ?>" <?php
                    }
                    ?> title="Sort by <?php
                    echo $field->Name;
                    ?> <?php
                    if ( $field->Name == $sortByField->Name && $sortByOrder = 'asc' ) {
                        ?> (descending)<?php
                    }
                    else if ( $field->Name == $sortByField->Name ) {
                        ?> (ascending)<?php
                    }
                    ?>"><?php
                    if ( isset( $primaryFields[ $field->Name ] ) ) {
                        ?><img src="images/key.png" alt="key" title="Primary Key" /> <?php
                    }
                    echo $field->Name;
                    ?></th><?php
                }
            ?></tr><?php
        }
    }

?>
